package main;

import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class DieRoll {
	public int dieValue;
	public int modifier = 0;
	public int mightUsed = 0;
	
	public Label valueLabel;
	public Button useMight;
	public Label noteLabel;
	
	public Button rerollButton;
	public boolean isRerollable = false;
	
	public Unit unitRolling;
	public String note = "";
	
	public UI myUI;
	
	public int x;
	public int y;
	
	public Random rng = new Random();
	
	public DieRoll(Unit unitRolling, int x, int y, int modifier, String note, UI myUI) {
		this.modifier = modifier;
		this.unitRolling = unitRolling;
		this.note = note;
		this.x = x;
		this.y = y;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	public DieRoll(Unit unitRolling, int x, int y, String note, UI myUI) {
		this.unitRolling = unitRolling;
		this.note = note;
		this.x = x;
		this.y = y;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	public DieRoll(Unit unitRolling, int x, int y, int modifier, UI myUI) {
		this.modifier = modifier;
		this.unitRolling = unitRolling;
		this.x = x;
		this.y = y;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	public DieRoll(Unit unitRolling, int x, int y, UI myUI) {
		this.unitRolling = unitRolling;
		this.x = x;
		this.y = y;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	
	public DieRoll(Unit unitRolling, int modifier, UI myUI) {
		this.modifier = modifier;
		this.unitRolling = unitRolling;
		this.x = -100;
		this.y = -100;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	public DieRoll(Unit unitRolling, UI myUI) {
		this.unitRolling = unitRolling;
		this.x = -100;
		this.y = -100;
		this.myUI = myUI;
		this.rollDie();
		this.createButton();
		myUI.diceOnTable.add(this);
	}
	
	protected void rollDie() {
		dieValue = rng.nextInt(6) + 1 + modifier;
		dieValue = Math.min(dieValue, 6);
		dieValue = Math.max(dieValue, 1);
		valueLabel = new Label(dieValue + " ");
		valueLabel.setName("dieRoll");
		valueLabel.setBounds(x,y,20,20);
		myUI.add(valueLabel);
	}
	
	protected void createButton() {
		useMight = new Button("+");
		useMight.setName("dieRoll");
		useMight.setBounds(x+20,y,20,20);
		useMight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mightUsed += 1;
				unitRolling.useMight(1);
				valueLabel.setText((dieValue + mightUsed) + " ");
				for (DieRoll roll : myUI.diceOnTable)
					roll.update();
				myUI.revalidate();
				myUI.repaint();
			}
		});
		myUI.add(useMight);
		if (this.dieValue + this.mightUsed >= 6 || unitRolling.getMight() <= 0)
			useMight.setEnabled(false);
		
		if (note != "") {
			noteLabel = new Label(note);
			noteLabel.setName("dieRoll");
			noteLabel.setBounds(x+50, y, 100, 20);
			noteLabel.setBackground(new Color(255,255,255,1));
			myUI.add(noteLabel);
		}
		
		myUI.revalidate();
		myUI.repaint();
	}
	
	public int getRoll() {
		return dieValue + mightUsed;
	}
	
	public boolean isModifiable() {
		return useMight.isEnabled() || isRerollable;
	}
	
	public void cleanup(){
		System.out.println("Cleaning up DieRoll for " + unitRolling.toChoiceString());
		myUI.remove(valueLabel);
		myUI.remove(useMight);
		if (note != "")
			myUI.remove(noteLabel);
		if (rerollButton != null)
			myUI.remove(rerollButton);
		myUI.diceOnTable.remove(this);
	}
	
	public void makeRerollable(){
		this.isRerollable = true;
		rerollButton = new Button();
		rerollButton.setBounds(x+50,y,60,20);
		rerollButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				isRerollable = false;
				reroll();
				rerollButton.setEnabled(false);
			}
		});
	}
	public void disableRerollable(){
		if (rerollButton != null)
			rerollButton.setEnabled(false);
		this.isRerollable = false;
	}
	
	public void reroll(){
		this.mightUsed = 0;
		this.cleanup();
		this.rollDie();
		this.createButton();
	}
	
	public void update(){
		if (this.dieValue + this.mightUsed >= 6 || unitRolling.getMight() <= 0)
			useMight.setEnabled(false);
	}
	
	public String toString() {
		return Integer.toString(this.getRoll());
	}
	
	public String toFullString() {
		String repr = "Unit rolling: " + this.unitRolling.toChoiceString() + "\n";
		repr += "Original roll (+modifier): " + this.dieValue + "\n";
		repr += "Modifier: " + this.modifier + "\n";
		repr += "Might used: " + this.mightUsed + "\n";
		return repr;
	}
}
